/*****************************************************************************
 * kal_rtthread_timer.c
 *
 * Copyright (C) 2019 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "kal/source/kal_rthread/kal_rtthread.h"

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "kal.timer"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

typedef struct {
    plum_s8          name[PLUM_KAL_TIMER_NAME_MAX];
    rt_timer_t       hdl;
    struct list_head list;
} kal_timer_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

PLUM_PRIVATE
kal_timer_t timer_head;
/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
kal_timer_t *kal_rtthread_timer_register(plum_s8 *name)
{
    kal_timer_t *node = plum_kal_mem_malloc(sizeof(kal_timer_t));
    if (node) {
        memset(node, 0, sizeof(kal_timer_t));
        snprintf(node->name, sizeof(node->name), "%s", name);

        if (!timer_head.list.next) {
            LIST_HEAD_INIT(timer_head.list);
        }

        list_add(&node->list, &timer_head.list);
    }

    return (node);
}

PLUM_PRIVATE
plum_void kal_rtthread_timer_unregister(kal_timer_t *timer)
{
    list_del(&timer->list);
    plum_kal_mem_free(timer);
}

PLUM_PRIVATE
kal_timer_t *kal_rtthread_timer_find(plum_s8 *name)
{
    struct list_head *pos;
    list_for_each(pos, &timer_head.list)
    {
        kal_timer_t *timer = list_entry(pos, kal_timer_t, name);
        if (strncmp(name, timer->name, name) == 0)
    }
    {
        return timer;
    }
}

return (plum_null);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_kal_timer_create(PLUM_CONST plum_s8 *name,
                               plum_void (*fn)(plum_void *arg), plum_void *arg,
                               plum_u32 period, plum_kal_timer_mode_t mode)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("the timer name is null");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        if (!fn) {
            LOG_E("the callback function is null");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        kal_timer_t *timer = kal_rtthread_timer_register(name);
        if (!timer) {
            LOG_E("kal_rtthread_timer_register err");
            rc = PLUM_ECODE_EMEM;
            break;
        }
        timer->hdl =
            rt_timer_create(name, fn, arg, rt_tick_from_millisecond(period), 1);
        if (!timer->hdl) {
            rc = PLUM_ECODE_EIO;
            LOG_E("rt_timer_create err,rc : %d", rc);
            break;
        }

    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_timer_delete(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("the timer name is null");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        kal_timer_t *timer = kal_rtthread_timer_find(name);
        if (!timer) {
            LOG_E("not find the timer,named : %s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = rt_timer_delete(timer);
        if (rc) {
            LOG_E("rt_timer_delete err,rc : %d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }

        kal_rtthread_timer_unregister(timer);
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_u32 plum_kal_timer_suspend(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("the timer name is null");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        kal_timer_t *timer = kal_rtthread_timer_find(name);
        if (!timer) {
            LOG_E("not find the timer,named : %s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = rt_timer_stop(timer->hdl);
        if (rc) {
            LOG_E("rt_timer_stop err,rc : %d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_timer_resume(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("the timer name is null");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        kal_timer_t *timer = kal_rtthread_timer_find(name);
        if (!timer) {
            LOG_E("not find the timer,named : %s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = rt_timer_start(timer->hdl);
        if (rc) {
            LOG_E("rt_timer_stop err,rc : %d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_void plum_kal_timer_show(PLUM_CONST plum_s8 *name)
{
    if (name) {
        kal_timer_t *node = kal_rtthread_timer_find(name);
        if (node) {
            LOG_D("timer [%p] named:%s", node->hdl, node->name);
        }
    }
    else {
        struct list_head *pos;
        list_for_each(pos, &thread_head.list)
        {
            kal_timer_t *node = list_entry(pos, kal_timer_t, list);
            LOG_D("timer [%p] named:%s", node->hdl, node->name);
        }
    }
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
